package cn.gailvlun.gll.widget

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cn.gailvlun.gll.R
import cn.gailvlun.gll.chatkit.message.MessageInput
import com.blankj.utilcode.util.FileUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.PictureSelectorActivity
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import rx_activity_result2.RxActivityResult

class CrossMessageInput : LinearLayout {

    private val mContext: Activity

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context as Activity

        View.inflate(context, R.layout.cross_message_input, this)

        orientation = LinearLayout.HORIZONTAL

        etMsg = findViewById(R.id.et_msg)
        ivImg = findViewById(R.id.iv_img)
        tvSend = findViewById(R.id.tv_send)

        tvSend?.setOnClickListener {
            onSubmitListener?.onSubmit(etMsg!!)
        }

        ivImg?.setOnClickListener {
            PictureSelector.create(mContext)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(1)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.SINGLE)
                    .previewImage(true)
                    .isCamera(true)
                    .imageFormat(PictureMimeType.PNG)
                    .isZoomAnim(true)
                    .sizeMultiplier(0.5f)
                    .setOutputCameraPath("/camera")
                    .previewEggs(true)
                    .compress(true)
                    .cropCompressQuality(50)
                    .minimumCompressSize(200)

            RxActivityResult.on(mContext)
                    .startIntent(Intent(mContext, PictureSelectorActivity::class.java))
                    .subscribe{activityResult ->
                        if (activityResult.resultCode() == Activity.RESULT_OK && onPictureSuccessListener != null) {
                            val list = PictureSelector.obtainMultipleResult(activityResult.data())
                            val localMedia = list[0]
                            val usefulPath: String
                            if (localMedia.compressPath != null) {
                                usefulPath = localMedia.compressPath
                            } else {
                                usefulPath = localMedia.path
                            }
                            onPictureSuccessListener?.onPictureSuccess(FileUtils.getFileByPath(usefulPath),
                                    localMedia.width, localMedia.height)
                        }
                    }
        }
    }

    var etMsg: EditText? = null
    var ivImg: ImageView? = null
    var tvSend: TextView? = null

    var onSubmitListener: MessageInput.OnSubmitListener? = null
    var onPictureSuccessListener: MessageInput.OnPictureSuccessListener? = null


}